﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using HandyControl.Controls;

namespace weighing
{
    /// <summary>
    /// HouseConfigWindow.xaml 的交互逻辑
    /// </summary>
    public partial class HouseConfigWindow : GlowWindow
    {
        #region var
        private List<ScaleHouseVo>? mHouseVos = new();
        #endregion

        public HouseConfigWindow()
        {
            InitializeComponent();
            CommonFunction.ShowCustomIcon(this);
        }
        #region window event
        private void GlowWindow_ContentRendered(object sender, EventArgs e)
        {
          
            SetDefaultDataSource();

            BindValue();
        }
        private void GlowWindow_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key == Key.Escape) this.Close();
        }
        #endregion
        private void SetDefaultDataSource()
        {
            this.houseCb.ItemsSource = null;
            var list = ScaleHouseDao.Instance.SelectList();
            mHouseVos = list?.Select((i) => { return new ScaleHouseVo(i); }).ToList();
            this.houseCb.ItemsSource = mHouseVos;
            if (mHouseVos?.Count <= 0)
            {
                alterTb.Visibility = Visibility.Visible;
            }
            else
            {
                alterTb.Visibility = Visibility.Collapsed;
            }
        }

        private void BindValue() { 
        
            if(App.InitConfig.scaleHouseId <= 0)
            {
                houseCb.SelectedIndex = -1;
            }
            else
            {
                for (int i = 0; i < mHouseVos?.Count; i++)
                {
                    var item = mHouseVos[i];
                    if (item.Id == App.InitConfig.scaleHouseId)
                    {
                        this.houseCb.SelectedIndex = i;
                        break;
                    }
                }              
            }
        }

        private void HouseCb_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (houseCb.SelectedIndex == -1)
            {
                return;
            }
         
            ScaleHouseVo? vo = this.houseCb.SelectedItem as ScaleHouseVo;
            if (vo == null) return; 

            if(vo.Id == App.InitConfig.scaleHouseId) { return; }
            var msg = $"是否要将 {vo.Name} 设置为当前系统的默认磅房";

            var result = CommonFunction.Ask(msg, this);

            if(result == MessageBoxResult.Yes)
            {
                App.InitConfig.scaleHouseId = vo.Id;              
                App.InitConfig.scaleHouseName = vo.Name;
                App.CurrentScaleHouseVo = vo;
                DelegeteFactory.Instance.OnDefaultScaleHouseChanged?.Invoke(vo);
                CommonFunction.AlertInfo("设置成功");
                InitConfigDao.Instance.Sqlite.Update(App.InitConfig);
            }
            else
            {
                for (int i = 0; i < mHouseVos?.Count; i++)
                {
                    var item = mHouseVos[i];
                    if (item.Id == App.InitConfig.scaleHouseId)
                    {
                        this.houseCb.SelectedIndex = i;
                        break;
                    }
                }
            }
        }
    }
}
